No. For example, the following is also an expression:
"This is" + " a string" + " expression"
Operator | Meaning | precedence |
---|---|---|
- | unary minus | highest |
+ | unary plus | highest |
* | multiplication | middle |
/ | division | middle |
% | remainder | middle |
+ | addition | low |
- | subtraction | low |
But arithmetic expressions are especially important. As you have seen, Java has many operators for arithmetic:
All of these operators can be used on floating point numbers and on integer numbers. (However, the % operator is rarely used on floating point.) For instance, / means "integer division" if both operands are integers, and means "floating point division" if one or both operands are floating point.
An integer operation is always done with 32 bits or more.
If one or both operand is 64 bits (data type long
) then the operation is
done with 64 bits.
Otherwise the operation is done with 32 bits, even if both operands are smaller.
With (for example) 16 bit short
variables,
the processor chip reads the value stored in the variable
into its 32 bit integer arithmetic unit.
The size of the variable does not have to correspond to the
number of bits the processor chip uses to do arithmetic.
For example:
short x = 12; // 16 bit short int result; // 32 bit int result = x / 3; // arithmetic will be done using 32 bits
For the expression x / 3
the computer will divide a
32 bit 12 by a 32 bit 3 and put the 32 bit answer in result .
The literal 3 automatically represents a 32 bit value.
Another example:
short x = 12; short y = 3; short result; result = x / y;
For the expression x / y
the computer will divide a
32 bit 12 by a 32 bit 3, even though the variables x and y are only 16 bit wide.
The answer will be shortened to 16 bits and then placed in
result.
At the professional programming level, details like these are sometimes important.
(To understand them in full you need a course like "Assembly Language"
or "Digital Systems.")
But mostly for general purpose programs you should use int
or
long
for integers and double
for floating point.
This will keep you out of trouble.